#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 20200212
# Project: qgggzy
import hashlib
import json
import math
import uuid
import pymysql
from datetime import datetime
from datetime import timedelta

from pyspider.libs.base_handler import *


class Handler(BaseHandler):
    crawl_config = {
        #'proxy':'127.0.0.1:3128',
        'headers': {
            'User-Agent:':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
        },
        'allow_nonstandard_methods': True
       
    }

    def __init__(self):
        self.db = pymysql.connect(host='106.13.85.158',port=3310,user='root', passwd='root@123', db='python', charset='utf8',use_unicode=True)

    @every(minutes=60)
    def on_start(self):
        self.crawl('http://www.cebpubservice.com/ctpsp_iiss/searchalltjaction/searchPortalNmae.do',
                   callback=self.start_get_data,data={'test':'test'},method="POST")
        
    def start_get_data(self,response):
        object = response.json['object']
        listBusiness = object['listBusiness']
        for businessJson in listBusiness:
            self.crawl('http://www.cebpubservice.com/ctpsp_iiss/searchbusinesstypebeforedooraction/getStringMethod.do?v='+businessJson['name'].strip(),
                   callback=self.query_page,data=self.assemblyData('1',businessJson['name'].strip()),save=businessJson,method="POST")
    #分页查询
    #任务的有效期。在此期间，该页面将被视为未修改。默认值：-1（从不重新抓取）  单位秒
    @config(age=0)
    def query_page(self, response):
        if response.json['success'] == True or response.json['success'] == 'true':
            object = response.json['object']
            page = object['page']
            totalPage = page['totalPage']
            business = response.save
            for index in range(int(totalPage)):
                self.crawl('http://www.cebpubservice.com/ctpsp_iiss/searchbusinesstypebeforedooraction/getStringMethod.do?v='+str(index+1),
                   callback=self.index_page,data=self.assemblyData(str(index+1),business['name'].strip()),save=business,method="POST") 
            
        
    #查询每一页的详情
    @config(age=0)
    def index_page(self, response):
        object = response.json['object']
        returnlist = object['returnlist']
        business = response.save
        for data in returnlist:
            #print(data)
            data['business'] = business['name']
            self.crawl('http://www.cebpubservice.com/ctpsp_iiss/SecondaryAction/findDetails.do?temp='+str(data['businessId']), 
                data=self.assemblyDetail(data),callback=self.detail_page,save=data,method="POST")
    def assemblyDetail(self,data):
        dict = {}
        dict['schemaVersion'] = data['schemaVersion']
        dict['businessKeyWord'] = 'tenderProject'
        dict['businessObjectName'] = data['businessObjectName']
        dict['tenderProjectCode'] = data['tenderProjectCode']
        dict['businessId'] = data['businessId']
        return dict
        
    #跳转到详情页面
    @config(priority=2)
    def detail_page(self,response):
        #print(response.json)
        html = ''
        if response.json['success'] == True or response.json['success'] == 'true':
            object = response.json['object']
            tenderProject = object['tenderProject']
            #print(tenderProject)
            for data in tenderProject:
                html += ("<div style='margin:0 auto 10px auto;text-align:center;font-size:14px;color:#333;font-family:微软雅黑;'>发布日期："+data['bulletinssueTime']+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>" \
                "<table class='table_tenderProject' border='0px' bordercolor='#999'>" \
                "<tr><td class ='even'><p>所属行业：<span>" + data['industriesType']
                + "</span></p></td><td><p>所属地区：<span>" + data['regionCode'] + "</span></p></td></tr>" \
                "<tr><td class ='even'><p>招标项目建立时间：<span>" + data['bulletinssueTime'] + "</span></p></td><td><p><span></span></p></td></tr>" \
                "<tr><td class ='even'><p>招标代理机构代码：<span title ='" + data['tenderAgencyCode'] + "'>" + data['tenderAgencyCode'] + "</span></p></td><td><p>招标代理机构名称：<span title ='" + data['tenderAgencyName']+ "'>" + data['tenderAgencyName'] + "</span></p></td></tr>" \
                "<tr><td class ='even'><p>招标人名称：<span title ='" + data['tendererName']+ "'>" + data['tendererName'] + "</span></p></td><td><p>招标组织方式：<span>" + data['tenderOrganizeForm']+ "</span></p></td></tr>" \
                "<tr><td class ='even'><p>行政监督部门代码：<span>" + data['superviseDeptCode'] + "</span></p></td><td><p>行政监督部门名称：<span>" + data['superviseDeptName'] + "</span></p></td></tr>" \
                "<tr><td class ='even'><p>行政审核部门代码：<span>" + data['approveDeptCode']+ "</span></p></td><td><p>行政审核部门名称：<span>" + data['approveDeptName'] + "</span></p></td></tr>" \
                "</table><div class ='div_tenderProject'>" \
                "<h5>招标内容与范围及招标方案说明：</h5><span>" + data['bulletinContent'] + "</span></div></div></div><div style='height:20px;'></div></div>")
        self.saveOrUpdate(response.save,html,tenderProject)
        return response.save
    # 保存或者更新	
    def saveOrUpdate(self,data,html,jsonText):
        #print(html)
        id = data['businessId']
        classify = ''
        classifyShow = pymysql.escape_string(data['business'])
        districtShow = pymysql.escape_string(data['regionName'])  #广东省 广州市
        platformName = pymysql.escape_string(data['transactionPlatfName'])
        stageName = '招标项目'
        stageShow = '招标项目'
        timeShow = data['receiveTime']
        title = data['businessObjectName']
        tradeShow = ''
        url = 'http://www.cebpubservice.com/ctpsp_iiss/SecondaryAction/findDetails.do'
        md5id = self.md5(id)
        selectSql = 'select * from T_RESULT_INFO t where t.source_id_md5 = "'+md5id+'"'
        currTime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        insertSql = 'insert into T_RESULT_INFO(id, source_id_md5,URL,TITLE, TYPE,UPDATE_USER,UPDATE_TIME,CREATE_USER,CREATE_TIME,UP_ID,UP_SOURCE_ID,CLASSIFY,CLASSIFY_SHOW,DISTRICT_SHOW,PLATFORM_NAME,STAGE_NAME,STAGE_SHOW,TIME_SHOW,TRADE_SHOW,HTML,JSON_TEXT) ' \
                  'values ("%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s")' % (
            id,md5id,url, pymysql.escape_string(title),'0',
            '','','',currTime,'','',classify,classifyShow,districtShow,platformName,stageName,stageShow,timeShow,tradeShow,pymysql.escape_string(html),jsonText);
        updateSql = 'update  T_RESULT_INFO set TITLE="%s", UPDATE_USER="%s",UPDATE_TIME="%s",UP_ID="%s",UP_SOURCE_ID="%s",HTML="%s",JSON_TEXT="%s" where source_id_md5="%s"' \
            '' % (pymysql.escape_string(title),'', currTime,'', '',pymysql.escape_string(html),jsonText, md5id);
        try:
            cursor = self.db.cursor()
            cursor.execute(selectSql)
            results = cursor.fetchall()
            print(results)
            if len(results) >0:
                print('update sql')
                cursor.execute(updateSql)
            else:
                print('insert sql')
                cursor.execute(insertSql)
            self.db.commit()
        except Exception as e:
            print(e)
            self.db.rollback()
    def assemblyData(self,pageNumber,business):
        data = {}
        data['searchName'] = ''
        data['searchArea'] = ''
        data["searchIndustry"] = business
        data["centerPlat"] = ''
        data["businessType"] = '招标项目'
        data["searchTimeStart"] = ''
        data["searchTimeStop"] = ''
        data["timeTypeParam"] = ''
        data["bulletinIssnTime"] = ''
        data["bulletinIssnTimeStart"] = ''
        data["bulletinIssnTimeStop"] = ''
        data["pageNo"] = pageNumber
        data["row"] = '15'
        return data
    def md5(self,str):
        m = hashlib.md5()
        m.update(str.encode("utf8"))
        return m.hexdigest()
